from ApplicationCommand import ApplicationCommand

from pyrfid.asn1.Encoder import Encoder
from pyrfid.asn1.Decoder import Decoder


class DeleteObjectCommand(ApplicationCommand):
    """docstring for ReadObjectCommand"""

    COMMAND_MODULE_OID = "28FC597E05"
    RESPONSE_MODULE_OID = "28FC597F05"
    listCmdAndData = list()
    
    def __init__(self, tagId, objectIdentifier, checkDuplicate):
        self.TAG_ID = tagId
        self.objectIdentifier = objectIdentifier
        self.CHECK_DUPLICATE = checkDuplicate

    def encode(self):
        encoder = Encoder()

        self.listCmdAndData.append(['06', '05', self.COMMAND_MODULE_OID])
        self.listCmdAndData.append(['30', '',  ''])
        
        #tagID
        tagIdHex = encoder.octetStrEncode(self.TAG_ID)
        lenTagIDHex = encoder.lengthEncode(len(tagIdHex) / 2)
        self.listCmdAndData.append([self.OCTET_STRING, lenTagIDHex, tagIdHex])

        #Object Identifier
        objectIdentifier = encoder.oidEncode(self.objectIdentifier)
        lenOIDHex = encoder.lengthEncode(len(objectIdentifier) / 2)
        self.listCmdAndData.append([self.OBJECT_IDENTIFIER, lenOIDHex, objectIdentifier])
        
        #Check Duplicate
        self.listCmdAndData.append([self.BOOLEAN , '01', encoder.boolEncode(self.CHECK_DUPLICATE)])

        lenData = len(lenTagIDHex)/2 + len(lenOIDHex)/2 + 1
        self.listCmdAndData[1][1] = str(encoder.lengthEncode(int(lenTagIDHex, 16) + int(lenOIDHex, 16) + 1))
        print self.listCmdAndData
        self._convertToHexString()

        return self.ENCODED_COMMAND


    def decode(self, encodedRSP):
        print "Decoder"
        if encodedRSP[4:14] != self.RESPONSE_MODULE_OID:
            raise 'ResponseTypeError'
        
        decoder = Decoder(encodedRSP)
        decodedArray = decoder.processer()
        #response
        decodedArray.pop(0)["value"]
        #30
        decodedArray.pop(0)["value"]
        
        completionCode = decodedArray.pop(0)["value"]
        executionCode = decodedArray.pop(0)["value"]
        return {'CompletionCode':completionCode, 'ExecutionCode':executionCode}
